route.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET } from "./route.js";
  11. describe("GET /api/branches/[branch]/[year]/[month]/days", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-days-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), {
  19. recursive: true,
  20. });
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it("returns 401 when unauthenticated", async () => {
  27. getSession.mockResolvedValue(null);
  28. const res = await GET(
  29. new Request("http://localhost/api/branches/NL01/2024/10/days"),
  30. { params: Promise.resolve({ branch: "NL01", year: "2024", month: "10" }) }
  31. );
  32. expect(res.status).toBe(401);
  33. expect(await res.json()).toEqual({
  34. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  35. });
  36. });
  37. it("returns 403 when branch user accesses a different branch", async () => {
  38. getSession.mockResolvedValue({
  39. role: "branch",
  40. branchId: "NL01",
  41. userId: "u1",
  42. });
  43. const res = await GET(
  44. new Request("http://localhost/api/branches/NL02/2024/10/days"),
  45. { params: Promise.resolve({ branch: "NL02", year: "2024", month: "10" }) }
  46. );
  47. expect(res.status).toBe(403);
  48. expect(await res.json()).toEqual({
  49. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  50. });
  51. });
  52. it("returns days for a valid branch/year/month when allowed", async () => {
  53. getSession.mockResolvedValue({
  54. role: "admin",
  55. branchId: null,
  56. userId: "u2",
  57. });
  58. const res = await GET(
  59. new Request("http://localhost/api/branches/NL01/2024/10/days"),
  60. { params: Promise.resolve({ branch: "NL01", year: "2024", month: "10" }) }
  61. );
  62. expect(res.status).toBe(200);
  63. expect(await res.json()).toEqual({
  64. branch: "NL01",
  65. year: "2024",
  66. month: "10",
  67. days: ["23"],
  68. });
  69. });
  70. it("returns 400 when any param is missing (authenticated)", async () => {
  71. getSession.mockResolvedValue({
  72. role: "admin",
  73. branchId: null,
  74. userId: "u2",
  75. });
  76. const res = await GET(
  77. new Request("http://localhost/api/branches/NL01/2024//days"),
  78. {
  79. params: Promise.resolve({
  80. branch: "NL01",
  81. year: "2024",
  82. month: undefined,
  83. }),
  84. }
  85. );
  86. expect(res.status).toBe(400);
  87. expect(await res.json()).toEqual({
  88. error: {
  89. message: "Missing required route parameter(s)",
  90. code: "VALIDATION_MISSING_PARAM",
  91. details: { params: ["month"] },
  92. },
  93. });
  94. });
  95. it("returns 404 when the month folder does not exist (authorized)", async () => {
  96. getSession.mockResolvedValue({
  97. role: "admin",
  98. branchId: null,
  99. userId: "u2",
  100. });
  101. const res = await GET(
  102. new Request("http://localhost/api/branches/NL01/2024/99/days"),
  103. { params: Promise.resolve({ branch: "NL01", year: "2024", month: "99" }) }
  104. );
  105. expect(res.status).toBe(404);
  106. expect(await res.json()).toEqual({
  107. error: {
  108. message: "Not found",
  109. code: "FS_NOT_FOUND",
  110. details: { branch: "NL01", year: "2024", month: "99" },
  111. },
  112. });
  113. });
  114. });